package https.handler;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;

import com.example.xuegengwang.xuegengwang.R;

import org.xutils.common.util.DensityUtil;

import common.Constant;
import common.MyApplication;
import https.InterNetController;
import https.bean.InterNetControllerBean;
import utils.MyLogUtils;

/**
 * Created by Administrator on 2017/3/6.
 * 带错误页面的handler
 */

public class ErrorPageHandler extends Handler {
    private static final String TAG = "MyHandler";
    private static View errorView;
    private static Button textView;
    private boolean canLoadErrorPage = false;//是否要弹出网络连接错误页

    public ErrorPageHandler() {
        this.canLoadErrorPage = true;
    }

    public ErrorPageHandler(boolean canLoadErrorPage) {
        this.canLoadErrorPage = canLoadErrorPage;
    }

    @Override
    public void handleMessage(Message msg) {
        if (msg.what == Constant.ERROR_CODE_FAILD) {
            if (errorView != null) {
                MyLogUtils.e(TAG, "删除之前的错误页面");
                //先去掉原有的errorView
                ViewGroup v = (ViewGroup) errorView.getParent();
                if (v != null) {
                    v.removeView(errorView);
                    errorView = null;
                }
            }
            if (canLoadErrorPage) {
                MyLogUtils.e(TAG, ErrorPageHandler.this.getClass().getName());
                final InterNetControllerBean interNetControllerBean = (InterNetControllerBean) msg.obj;
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
                layoutParams.topMargin = DensityUtil.px2dip(MyApplication.getContext().getResources().getDimension(R.dimen.top_height));
                if (errorView == null) {
                    MyLogUtils.e(TAG, "创建返回错误页面");
                    errorView = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.error_page, null);
                    errorView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MyLogUtils.e(TAG, "别点我这个错误页面啊");
                        }
                    });
                    textView = (Button) errorView.findViewById(R.id.error_tv);
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            new InterNetController(interNetControllerBean.getContext(), interNetControllerBean.getUrl(), ErrorPageHandler.this, interNetControllerBean.getDataMap(), interNetControllerBean.getFlag());
                        }
                    });
                }
                MyLogUtils.e(TAG, "是否网络连接错误呢？：" + interNetControllerBean.isNetworkError());
                if (interNetControllerBean.isNetworkError()) {
                    ((Activity) interNetControllerBean.getContext()).addContentView(errorView, layoutParams);
                }
            }
        } else if (msg.what != Constant.ERROR_CODE_FINISH) {
            if (errorView != null) {
                ViewGroup v = (ViewGroup) errorView.getParent();
                if (v != null) {
                    MyLogUtils.e(TAG, "onFinish+++++删除页面");
                    v.removeView(errorView);
                    errorView = null;
                }
            }
        }
        super.handleMessage(msg);
    }

}
